package com.yzh.springboot_rbac.controller;


import com.yzh.springboot_rbac.entity.DictData;
import com.yzh.springboot_rbac.service.DictDataService;
import com.yzh.springboot_rbac.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典数据管理菜单页
 */
@RestController
@RequestMapping("/DictData")
public class DictDataController {
    @Autowired
    private DictDataService dictDataService;
    /**
     * 1.根据类型查询字典数据
     */
    @GetMapping("/selectDictDataByType/{dictType}")
    public AjaxResult selectDictDataByType(@PathVariable String dictType) {
        List<DictData> dictData = dictDataService.selectDictDataByType(dictType);
        return AjaxResult.success(dictData);
    }


    /**
     * 2.通过条件查询字典数据
     * (后续往里面加分页）
     * 这个地方在controller层判断是否为空，省去了在service多判断一次，更简便。
     * 要不然controller和service都要判断
     */
    @PostMapping("/selectDictDataByCondition")
    public AjaxResult selectDictDataByCondition(@RequestBody DictData dictData) {
        List<DictData> dictDataList = dictDataService.selectDictDataByCondition(dictData);
        if (dictDataList.isEmpty()) {
            return AjaxResult.error();
        } else {
            return AjaxResult.success(dictDataList);
        }
    }

    /**
     * 3.添加字典数据
     */
    @PostMapping("/addDictData")
    public AjaxResult addDictData(@RequestBody DictData dictData) {
        Boolean flag = dictDataService.isAdd(dictData);
        if (flag) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 4.批量删除字典数据
     */
    @DeleteMapping("/deleteDictData/{dictCodes}")
    public AjaxResult deleteDictData(@PathVariable("dictCodes") Long[] dictCodes) {
        dictDataService.deleteDictDataByIds(dictCodes);
        return AjaxResult.success();
    }

    /**
     * 5.更新字典数据
     */
    @PutMapping("/updateDictData")
    public AjaxResult updateDictData(@RequestBody DictData dictData) {
        int row = dictDataService.updateDictData(dictData);
        if (row > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}
